package com.freexiaoyu.cloud.utils;

import java.math.BigInteger;

/*******************************
 * @author Free
 * @emil Freexiaoyu@foxmail.com
 * create Time 2020-11-12 10:58
 * describe  
 * modify by 
 * modify Time
 * modify desc   
 ***********************************/
public class ConvertString {


    /***
     * 取反
     * @param str
     * @return
     */
    public static String stringFlips(String str){
        byte [] bytes = hexStringToByte(str);
        byte temp;
        for(int i=0;i<bytes.length;i++){
            temp = bytes[i];
            bytes[i] = (byte) (~temp);
        }
        String bths =  bytesToHexString(bytes);
        return bths ;
    }

    /**
     * 将数字字符串转化为二进制byte数组
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
    /**
     * 将二进制数组转化为16进制字符串
     * @param src
     * @return
     */
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }


    /**
     * 字符串转换成十六进制字符串
     *
     * @param str 待转换的ASCII字符串
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     */
    public static String str2HexStr(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * 将16进制字符串转换为byte[]
     *
     * @param str
     * @return
     */
    public static byte[] toBytes(String str) {
        if (str == null || str.trim().equals("")) {
            return new byte[0];
        }
        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }
        return bytes;
    }



    //將10進制轉換為16進制
    public static String encodeHEX(Integer numb) {
        String hex = Integer.toHexString(numb);
        return hex;

    }

    //將10進制轉換為16進制
    public static String encodeHEX(Long numb) {
        String hex = Long.toHexString(numb);
        return hex;

    }

    //將16進制字符串轉換為10進制數字
    public static int decodeHEX(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        int numb = bigint.intValue();
        return numb;
    }

    //將16進制字符串轉換為10進制數字
    public static long decodeHEX2(String hexs) {
        BigInteger bigint = new BigInteger(hexs, 16);
        long numb = bigint.longValue();
        return numb;
    }

}
